package org.nlp2rdf.navigator.client.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * owl:Thing and owl:Nothing are always removed
 * 
 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
 * 
 */
@SuppressWarnings("serial")
public class RelatedConceptsDTO extends DTO {

	private List<StrategyDTO> strategies = new ArrayList<StrategyDTO>();

	@Override
	public String toString() {

		int total = 0;
		for (StrategyDTO s : strategies) {
			total += s.getConcepts().size();
		}

		return strategies.size() + " strategies with " + total + " concepts";
	}

	public String toDebugString() {

		StringBuffer ret = new StringBuffer();
		for (StrategyDTO s : strategies) {
			ret.append(s.toDebugString()+"\n****************");
		}

		return super.toString() + "\n" + ret.toString();
	}

	public List<StrategyDTO> getStrategies() {
		return strategies;
	}

	/**
	 * owl:Thing and owl:Nothing are always removed
	 * 
	 * @param strategies
	 */
	public void setStrategies(List<StrategyDTO> strategies) {
		this.strategies = strategies;
	}

	public void addStrategy(StrategyDTO s) {
		this.strategies.add(s);
	}

	/**
	 * 
	 * @author Sebastian Hellmann <hellmann@informatik.uni-leipzig.de>
	 * 
	 */
	public static class StrategyDTO implements Serializable {

		String[] remove = new String[] { "http://www.w3.org/2002/07/owl#Thing",
				"http://www.w3.org/2002/07/owl#Nothing" };
		{
			Arrays.sort(remove);
		}
		private String title;
		private List<ConceptDTO> concepts = new ArrayList<ConceptDTO>();

		// private List<StoredConceptDTO> storedConcepts = new
		// ArrayList<StoredConceptDTO>();

		@Override
		public String toString() {
			return title + " normal/stored concepts: " + getNormalConcepts().size() + " / "
					+ getStoredConcepts().size() + " ";
		}

		public String toDebugString() {
			return title + "\nnormal/stored concepts: " + getNormalConcepts().size() + " / "
			+ getStoredConcepts().size() + "\nNormal:" + getNormalConcepts() + "\nStored:" + getStoredConcepts();
		}

		/**
		 * owl:Thing and owl:Nothing are always removed
		 * 
		 * @param learningResultDTO
		 */
		public void addConcept(ConceptDTO c) {
			if (contains(c, remove)) {
				return;
			}
			 concepts.add(c);
			// if (c instanceof StoredConceptDTO) {
			// StoredConceptDTO s = (StoredConceptDTO) c;
			// // storedConcepts.add(s);
			//
			// } else {
			// // concepts.add(c);
			// }
		}

		// public void setAllConcepts(List<ConceptDTO> concepts) {
		// this.concepts.clear();
		// this.storedConcepts.clear();
		// for (ConceptDTO l : concepts) {
		// addConcept(l);
		// }
		// }

		// public List<ConceptDTO> getAllConcepts() {
		// List<ConceptDTO> l = new ArrayList<ConceptDTO>();
		// l.addAll(concepts);
		// // l.addAll(storedConcepts);
		// return l;
		// }

		private boolean contains(ConceptDTO learningResultDTO, String[] remove) {
			return (Arrays.binarySearch(remove, learningResultDTO.getManchesterSyntaxString()) >= 0);
		}

		public List<StoredConceptDTO> getStoredConcepts() {
			List<StoredConceptDTO> ret = new ArrayList<StoredConceptDTO>();
			for (ConceptDTO c : concepts) {
				if (c instanceof StoredConceptDTO) {
					ret.add((StoredConceptDTO) c);
				}
			}
			return ret;
		}

		public List<ConceptDTO> getNormalConcepts() {
			List<ConceptDTO> ret = new ArrayList<ConceptDTO>();
			for (ConceptDTO c : concepts) {
				if (!(c instanceof StoredConceptDTO)) {
					ret.add(c);
				}
			}
			return ret;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public List<ConceptDTO> getConcepts() {
			return concepts;
		}

		public void setConcepts(List<ConceptDTO> concepts) {
			this.concepts = concepts;
		}

	}
}
